home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.1 / Examples1 / asl / edithook.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  2.9 KB  |  124 lines

  1. /*
  2. COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992-1999
  3. Amiga, Inc.  All rights reserved.
  4.  
  5. DISCLAIMER: This software is provided "as is".  No representations or
  6. warranties are made with respect to the accuracy, reliability, performance,
  7. currentness, or operation of this software, and all use is at your own risk.
  8. Neither Amiga nor the authors assume any responsibility or liability
  9. whatsoever with respect to your use of this software.
  10. */
  11.  
  12.  
  13. /****************************************************************************/
  14.  
  15.  
  16. #include <exec/types.h>
  17. #include <intuition/intuition.h>
  18. #include <intuition/gadgetclass.h>
  19. #include <intuition/sghooks.h>
  20. #include <devices/inputevent.h>
  21. #include <utility/hooks.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24.  
  25. #include <clib/utility_protos.h>
  26.  
  27. #include <pragmas/utility_pragmas.h>
  28.  
  29. #include "edithook.h"
  30.  
  31.  
  32. /*****************************************************************************/
  33.  
  34.  
  35. #define    RETURN     (0x44)
  36. #define    TAB     (0x42)
  37.  
  38.  
  39. /*****************************************************************************/
  40.  
  41.  
  42. extern struct Library *UtilityBase;
  43.  
  44.  
  45. /*****************************************************************************/
  46.  
  47.  
  48. /* Check whether the buffer only contains a valid hex number... */
  49. BOOL CheckHex(STRPTR buffer, ULONG *value)
  50. {
  51. char  ch;
  52. ULONG num;
  53. UWORD i;
  54.  
  55.     if ((buffer[0] == '0') && (buffer[1] == 'x'))
  56.     {
  57.         num = 0;
  58.         i   = 2;
  59.         while (buffer[i])
  60.         {
  61.             ch  = ToUpper(buffer[i]);
  62.             num = num * 16;
  63.             if ((ch >= 'A') && (ch <= 'F'))
  64.                 num += ch - 'A' + 10;
  65.             else if ((ch >= '0') && (ch <= '9'))
  66.                 num += ch - '0';
  67.             else
  68.                 return(FALSE);
  69.  
  70.             i++;
  71.         }
  72.         *value = num;
  73.         return(TRUE);
  74.     }
  75.  
  76.     return(FALSE);
  77. }
  78.  
  79.  
  80. /*****************************************************************************/
  81.  
  82.  
  83. /* Function used as a hexadecimal editing hook for a string gadget. Only
  84.  * allows hex numbers to be entered in a string gadget. The current value of
  85.  * the hex number is stored in the gadget's UserData field.
  86.  */
  87. ULONG __asm HexHook(register __a0 struct Hook *hook,
  88.                     register __a2 struct SGWork *sgw,
  89.                     register __a1 ULONG *msg)
  90. {
  91. BOOL  exiting = FALSE;
  92. ULONG result;
  93.  
  94.     geta4();
  95.  
  96.     result = 0;
  97.     if (*msg == SGH_KEY)
  98.     {
  99.         result = 1;
  100.         switch (sgw->IEvent->ie_Code)
  101.         {
  102.             case TAB   :
  103.             case RETURN: exiting = TRUE;
  104.         }
  105.  
  106.         if (CheckHex(sgw->WorkBuffer, (ULONG *)&sgw->Gadget->UserData))
  107.         {
  108.             if (exiting)
  109.             {
  110.                 sprintf(sgw->WorkBuffer,"0x%08lX",sgw->Gadget->UserData);
  111.                 sgw->NumChars  = strlen(sgw->WorkBuffer);
  112.                 sgw->BufferPos = 0;
  113.             }
  114.         }
  115.         else
  116.         {
  117.             sgw->EditOp  = EO_BADFORMAT;
  118.             sgw->Actions = SGA_BEEP;
  119.         }
  120.     }
  121.  
  122.     return (result);
  123. }
  124.